package com.epfresh.supplier.activity.standingbook;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aigestudio.wheelpicker.core.AbstractWheelPicker;
import com.epfresh.api.widget.WheelDatePicker;
import com.epfresh.supplier.R;

/**
 * Created by shenjie on 2017/9/19.
 */

public class DatePickerPopupWindow extends PopupWindow implements View.OnClickListener{
    private Context context;
    private DatePickerListener listener;
    private TextView cancel,ok;
    private WheelDatePicker datePicker;
    private RelativeLayout cc;
    private String dataStr;
    private LinearLayout bottom;

    public boolean isNeed() {
        return need;
    }

    public void setNeed(boolean need) {
        this.need = need;
    }

    private boolean need;

    private int padding;
    private int textSize;
    private int itemSpace;
    public  DatePickerPopupWindow(Context context){
        this.context=context;
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        setAnimationStyle(R.style.popwin_bottom_style);
        initView();
    }

    private void initView(){
        padding = context.getResources().getDimensionPixelSize(R.dimen.WheelItemSpace);
        textSize = context.getResources().getDimensionPixelSize(R.dimen.text_size_mid_16);
        itemSpace = context.getResources().getDimensionPixelSize(R.dimen.ItemSpaceLarge);
        View view= LayoutInflater.from(context).inflate(R.layout.datepicker_window,null);
        setContentView(view);
        cc= (RelativeLayout) view.findViewById(R.id.head);
        cc.setOnClickListener(this);
        cancel= (TextView) view.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(this);
        bottom= (LinearLayout) view.findViewById(R.id.bottom);
        ok= (TextView) view.findViewById(R.id.tv_ok);
        ok.setOnClickListener(this);
        datePicker= (WheelDatePicker) view.findViewById(R.id.datepicker);
        datePicker.setBackgroundColor(0xFFffffff);
        datePicker.setTextColor(0xFF626262);
        datePicker.setCurrentTextColor(0xFF313131);
        datePicker.setLabelColor(0xFF333333);
        datePicker.setLabelTextSize(textSize/2);
        datePicker.setTextSize(textSize);
        datePicker.setItemSpace(itemSpace);
        datePicker.setOnWheelChangeListener(new AbstractWheelPicker.OnWheelChangeListener() {
            @Override
            public void onWheelScrolling(float deltaX, float deltaY) {

            }

            @Override
            public void onWheelSelected(int index, String data) {
                dataStr=data;
            }

            @Override
            public void onWheelScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head:
            case R.id.tv_cancel:
                need=false;
                viewOut();
                break;
            case R.id.tv_ok:
                need=true;
                viewOut();
                break;
        }
    }

    public  interface DatePickerListener{
        public void select(String date);
    }

    public DatePickerListener getListener() {
        return listener;
    }

    public void setListener(DatePickerListener listener) {
        this.listener = listener;
    }

    public  void viewIn() {
        bottom.setVisibility(View.VISIBLE);
        TranslateAnimation tran=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0);
        tran.setDuration(200);
        bottom.setAnimation(tran);
    }
    public  void viewOut(){
        bottom.setVisibility(View.INVISIBLE);
        TranslateAnimation tran=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1);
        tran.setDuration(200);
        tran.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
                if(listener!=null&&need){
                    listener.select(dataStr);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        bottom.setAnimation(tran);
    }
}
